package com.thirdDay;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {

        ArrayList<Student> arrayList = new ArrayList<>();

        while (true) {
            System.out.println("-------- 欢迎来到学会说呢过管理系统 --------");
            System.out.println("1: 添加学生");
            System.out.println("2: 删除学生");
            System.out.println("3: 修改学生");
            System.out.println("4: 查询学生");
            System.out.println("5: 退出");
            System.out.println("请输入你的选择");

            Scanner sc = new Scanner(System.in);
            String next = sc.next();

            if (next.equals("1")) {
                // System.out.println("添加学生");
                addStudent(arrayList);
            } else if (next.equals("2")) {
                // System.out.println("删除学生");
                deleteStudent(arrayList);
            } else if (next.equals("3")) {
                // System.out.println("修改学生");
                updateStudent(arrayList);
            } else if (next.equals("4")) {
                // System.out.println("查询学生");
                queryStudent(arrayList);
            } else if (next.equals("5")) {
                System.out.println("退出");
                break;
            } else {
                System.out.println("没有这个选项");
            }
        }
    }


    /**
     * 添加学生
     *
     * @param arrayList
     */
    private static void addStudent(ArrayList<Student> arrayList) {
        Scanner scanner = new Scanner(System.in);

        String id = null;
        while (true) {
            System.out.println("请输入学生的id");
            id = scanner.next();
            boolean flag = contains(arrayList, id);
            if (flag) {
                System.out.println("id已经存在,请重新录入");
            } else {
                break;
            }
        }

        System.out.println("请输入学生的姓名");
        String name = scanner.next();
        System.out.println("请输入学生的年龄");
        int age = scanner.nextInt();
        System.out.println("请输入学生的家庭住址");
        String address = scanner.next();
        Student student = new Student(id, name, age, address);
        arrayList.add(student);

        // 提示一下用户
        System.out.println("学生信息添加成功");
    }


    /**
     * 删除学生
     */
    public static void deleteStudent(ArrayList<Student> list) {
        // System.out.println("删除学生");

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();

        // 查询id在集合中的索引
        int index = getIndex(list, id);
        // 对index进行判断
        // 如果-1,就表示不存在,结束方法,回到初始菜单
        if (index >= 0) {
            // 如果大于等于0,表示存在,直接删除
            list.remove(index);
            System.out.println("id为:" + id + "的学生删除成功");
        } else {
            System.out.println("id不存在,删除失败");
        }
    }


    /**
     * 修改学生
     */
    public static void updateStudent(ArrayList<Student> list) {
        // System.out.println("修改学生");

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        String id = sc.next();

        int index = getIndex(list, id);

        if (index == -1) {
            System.out.println("要修改的id" + id + "不存在,请重新输入");
            return;
        }

        // 表示id是存在的
        Student student = list.get(index);

        // 输入其他的信息并修改
        System.out.println("请输入要修改的学生姓名");
        String newName = sc.next();
        student.setName(newName);

        System.out.println("请输入要修改的学生年龄");
        int newAge = sc.nextInt();
        student.setAge(newAge);

        System.out.println("请输入要修改的学生地址");
        String newAddress = sc.next();
        student.setAddress(newAddress);

        System.out.println("学生信息修改成功");
    }


    /**
     * 查询学生
     */
    private static void queryStudent(ArrayList<Student> arrayList) {
        if (arrayList.size() == 0) {
            System.out.println("当前无学生信息,请添加后再查询");
        }

        // 当代码执行到这里,表示集合中是有数据的
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());
        }
    }


    /**
     * 判断id在集合中是否存在
     *
     * @param list
     * @param id
     * @return
     */
    public static boolean contains(ArrayList<Student> list, String id) {
        // 循环遍历集合得到里面的每一个学生对象
        // 拿到学生对象后,获取id并进行判断
        // 存在,true    不存在false
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            String studentId = student.getId();
            if (studentId.equals(id)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 通过id获取索引的方法
     */
    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            String studentId = student.getId();
            if (studentId.equals(id)) {
                return i;
            }
        }

        return -1;
    }
}
